<script setup>
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ChannelSelect from './components/ChannelSelect.vue'
import request from '@/utils/request'
import { formatTime } from '@/utils/format.js'
import ArticleEdit from './components/ArticleEdit.vue'
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/stores'
// 数据
const articleList = ref([]) // 文章列表
const total = ref(0) // 总条数
const loading = ref(false) // loading状态

const articleEditRef = ref()
// 添加逻辑 显示弹层
const onAddArticle = () => {
  articleEditRef.value.open({})
}

// 编辑
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}
// 删除
const onDeleteArticle = (row) => {
  request
    .get('/employee/del', {
      params: { id: row.id }
    })
    .then(() => {
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
      getArticleList()
    })
}

//定义请求参数对象
const params = ref({
  // 第一页
  pagenum: 1,
  // 每页5条
  pagesize: 10,
  cate_id: '',
  state: ''
})
// 基于 params 参数，获取文章列表
const getArticleList = async () => {
  loading.value = true
  request
    .post('/employee/page', {
      organizationId: params.value.cate_id || '',
      pageQuery: {
        limit: params.value.pagesize,
        page: params.value.pagenum
      },
      searchName: params.value.state
    })
    .then((res) => {
      loading.value = false
      articleList.value = res.data
      total.value = res.total
      loading.value = false
    })
}
getArticleList()

// 处理分页逻辑
const onSizeChange = (size) => {
  // 每页条数变化了，正在访问的当前页意义不大，数据不在原来那一页
  //从第一页渲染即可
  params.value.pagenum = 1
  //更改每页条数
  params.value.pagesize = size
  //刷新
  getArticleList()
}
//更新当前页
const onCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}

// 搜索和重置
// 搜索：按照最新条件，重新检索，从第一页开始展示
const onSearch = () => {
  //重置页面
  params.value.pagenum = 1
  getArticleList()
}
// 重置，将筛选条件清空，从第一页开始展示
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

// 添加修改成功
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}

//获取登录用户
const { loginUserInfo } = storeToRefs(useUserStore())
const userInfo = ref({
  name: loginUserInfo.value.userName
})
</script>

<template>
  <page-container title="员工管理">
    <!-- 具名插槽写按钮 -->
    <template #extra v-if="userInfo.name == 'admin'">
      <el-button type="primary" @click="onAddArticle"> 添加员工 </el-button>
    </template>

    <!-- 表单区域 inline：在一行中显示-->
    <el-form inline>
      <!-- el-form-item 一行 -->
      <el-form-item label="部门：">
        <!-- 下拉菜单和上面 params 绑定 -->
        <channel-select
          style="width: 240px"
          v-model="params.cate_id"
        ></channel-select>
      </el-form-item>
      <el-form-item label="用户名：">
        <!-- 下拉菜单和上面 params 绑定 -->
        <el-input v-model="params.state" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table
      :data="articleList"
      style="width: 100%"
      height="500"
      v-loading="loading"
    >
      <!-- 作用域插槽，可以自己拿到想要的数据 -->
      <el-table-column label="员工姓名" width="400">
        <template #default="{ row }">
          <!-- el-link 链接效果 -->
          <el-link type="primary" :underline="false">{{
            row.userName
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="部门" prop="organizationName"></el-table-column>
      <el-table-column label="底薪" prop="basicSalary"></el-table-column>
      <el-table-column label="手机号" prop="phone"></el-table-column>
      <el-table-column label="更新时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.dateOnBoard) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" prop="state">
        <template #default="{ row }">
          {{ row.state === '0' ? '在职' : '离职' }}
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="100" v-if="userInfo.name == 'admin'">
        <!-- 解构row，后面通过其id进行操作 -->
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[10, 20, 50, 100]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 25px; justify-content: flex-end"
    />

    <!-- 添加编辑抽屉 -->
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>

<style lang="scss" scoped></style>
